package cn.com.cifi.mars.web.action.report;

import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import cn.com.cifi.mars.bean.vo.supply.SupplyMonthlyDetailExcelVO;
import cn.com.cifi.mars.bean.vo.supply.SupplyMonthlyDetailVO;
import cn.com.cifi.mars.bean.vo.supply.SupplyPlanVersionCompareExcelVO;
import cn.com.cifi.mars.bean.vo.supply.SupplyPlanVersionCompareVO;
import cn.com.cifi.mars.common.base.BaseAction;
import cn.com.cifi.mars.common.bean.R;
import cn.com.cifi.mars.constant.OrgTypeEunm;
import cn.com.cifi.mars.entity.common.IdmUser;
import cn.com.cifi.mars.service.DmDmSupplyCompareDetailService;
import cn.com.cifi.mars.service.DmDmSupplyMonthlyDetailService;
import cn.com.cifi.mars.service.SupplyConfirmRecordDetailService;
import cn.com.cifi.mars.util.DateUtils;
import cn.com.cifi.mars.util.LocalDateTimeUtils;
import cn.com.cifi.mars.util.ReportUtils;
import cn.com.cifi.mars.util.StrUtils;
import cn.com.cifi.mars.util.SupplyCompareReportTemplate;
import cn.com.cifi.mars.util.SupplyMonthlyDeatilReportTemplate;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @Description：经营分析-供货计划对比报表实现
 * @author：yyd
 * @date：2020年2月18日 下午8:14:15
 */
@Slf4j
@RestController
public class SupplyReportController extends BaseAction implements ISupplyReportController {
	
	@Autowired
	private DmDmSupplyCompareDetailService dmDmSupplyCompareDetailService;
	
	@Autowired
	private DmDmSupplyMonthlyDetailService dmDmSupplyMonthlyDetailService;
	
	@Autowired
	private SupplyConfirmRecordDetailService supplyConfirmRecordDetailService;
	
	@Override
	public R initSupplyCompareReport(String apiVersion,String yearMonth) {
		log.info("###############初始化供货对比表   Start: "+ LocalDateTime.now());
		long start = System.currentTimeMillis();
		dmDmSupplyCompareDetailService.initSupplyCompareData(yearMonth);
		long end = System.currentTimeMillis();
		long diff = end-start;
		log.info("###############初始化供货对比表   End: "+ LocalDateTime.now());
		return ok("运行时长（毫秒）："+diff);
	}
	
	@Override
	public R initSupplyMonthlyDetailReport(String apiVersion,String yearMonth,String projectId) {
		log.info("###############初始化月度供货明细表   Start: "+ LocalDateTime.now());
		long start = System.currentTimeMillis();
		dmDmSupplyMonthlyDetailService.initSupplyMonthlyDetailReport(yearMonth,projectId);
		long end = System.currentTimeMillis();
		long diff = end-start;
		log.info("###############初始化月度供货明细表   End: "+ LocalDateTime.now());
		return ok("运行时长（毫秒）："+diff);
	}
	
	@Override
	public R handleInvestment(String apiVersion, String versionId) {
		return ok(dmDmSupplyMonthlyDetailService.handleInvestment(versionId));
	}
	
	@Override
	public R projectParentLevel(String apiVersion, String projectId) {
		return ok(dmDmSupplyCompareDetailService.getProjectParentLevel(projectId));
	}
	
	@Override
	public R supplyCompareReport(String apiVersion, String deadline) {
		String userName = getOperator().getUsername();
		List<SupplyPlanVersionCompareVO> list = dmDmSupplyCompareDetailService.getCompareReport(deadline,userName);
		if(CollectionUtils.isEmpty(list)) {
			return ok(list);
		}
		List<SupplyPlanVersionCompareVO> resList = sortSupplyPlanVersionCompareStage(list);
		return ok(resList);
	}
	
	private List<SupplyPlanVersionCompareVO> sortSupplyPlanVersionCompareStage(List<SupplyPlanVersionCompareVO> resList) {
		List<SupplyPlanVersionCompareVO> stageList = Collections.synchronizedList(new ArrayList<>());
		List<SupplyPlanVersionCompareVO> lastList = Collections.synchronizedList(new ArrayList<>());
		resList.forEach(x->{
			if(x.getType().equals(OrgTypeEunm.ORG_STAGE.getKey())) {
				stageList.add(x);
			}else {
				lastList.add(x);
			}
		});
		if(CollectionUtils.isNotEmpty(stageList)){
			List<SupplyPlanVersionCompareVO> sortList = stageList.parallelStream().sorted((a, b) -> {
			    //分期
			    String aStageName = StrUtils.nvl(a.getName());
			    String bStageName = StrUtils.nvl(b.getName());
			    if (!aStageName.equals(bStageName)) {
			    	return ReportUtils.compareStrStartedChinaNum(aStageName, bStageName);
			    }
			    return 0;
			}).collect(Collectors.toList());
			lastList.addAll(sortList);
         }
		return lastList;
	}

	@Override
	public void supplyCompareReportExport(String apiVersion, String deadline, HttpServletResponse response) {
		log.info("###############供货对比表导出   Start: "+ LocalDateTime.now());
		String userName = getOperator().getUsername();
		List<SupplyPlanVersionCompareVO> list = dmDmSupplyCompareDetailService.getCompareReport(deadline,userName);
		List<SupplyPlanVersionCompareExcelVO> resList = Collections.synchronizedList(new ArrayList<>());
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyPlanVersionCompareVO vo : list) {
				SupplyPlanVersionCompareExcelVO excelVo = new SupplyPlanVersionCompareExcelVO();
				String  type = vo.getType();
				excelVo.setId(vo.getId());
				excelVo.setCode(vo.getCode());
				excelVo.setName(vo.getName());
				excelVo.setPid(vo.getPid());
				excelVo.setIsParking(vo.getIsParking());
				if(type.equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
					excelVo.setType(type);
					excelVo.setProductTypeName(vo.getProductTypeName());
					excelVo.setProductTypeCode(vo.getProductTypeCode());
					excelVo.setProductName(vo.getName());
					excelVo.setProductCode(vo.getCode());
					excelVo.setBusinessTypeName(vo.getBusinessTypeName());
					excelVo.setFreeTypeName(vo.getFreeTypeName());
					excelVo.setProTypeName(vo.getProTypeName());
					
					//产品构成所有父级信息
					prepareCompareParentInfo(list,vo.getPid(),excelVo,OrgTypeEunm.ORG_PRODUCT.getKey());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_GROUP.getKey())) {
					excelVo.setType(type);
					excelVo.setGroupName(vo.getGroupName());
					//组团所有父级信息
					prepareCompareParentInfo(list,vo.getPid(),excelVo,OrgTypeEunm.ORG_GROUP.getKey());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_STAGE.getKey())) {
					excelVo.setType(type);
					excelVo.setStageName(vo.getStageName());
					excelVo.setStageCode(vo.getStageCode());
					excelVo.setEquityRatio(vo.getEquityRatio());
					excelVo.setTradersWay(vo.getTradersWay());
					excelVo.setIsMergeTable(vo.getIsMergeTable());
					excelVo.setPendingHomeSale(vo.getPendingHomeSale());
					//分期所有父级信息
					prepareCompareParentInfo(list,vo.getPid(),excelVo,OrgTypeEunm.ORG_STAGE.getKey());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_PRODUCTF.getKey())) {
					excelVo.setType(type);
					excelVo.setProjectName(vo.getProjectName());
					excelVo.setProjectCode(vo.getProjectCode());
					excelVo.setIsFirst(vo.getIsFirst());
					//项目所有父级信息
					prepareCompareParentInfo(list,vo.getPid(),excelVo,OrgTypeEunm.ORG_PRODUCTF.getKey());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_CITY_COMPANY.getKey())) {
					excelVo.setType(type);
					excelVo.setCityName(vo.getCityName());
					//城市公司所有父级信息
					prepareCompareParentInfo(list,vo.getPid(),excelVo,OrgTypeEunm.ORG_CITY_COMPANY.getKey());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_BUSINESS_DIVISION.getKey())){
					//区域
					excelVo.setType(type);
					excelVo.setAreaName(vo.getAreaName());
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}else if(type.equals(OrgTypeEunm.ORG_WANDA_GROUP.getKey())){
					excelVo.setType(type);
					excelVo.setAreaName("集团合计");
					excelVo.setYearValue(vo.getYearValue());
					excelVo.setFirstMonthlyValue(vo.getFirstMonthlyValue());
					excelVo.setFirstHalfYearValue(vo.getFirstHalfYearValue());
					excelVo.setSecondHalfYearValue(vo.getSecondHalfYearValue());
					excelVo.setSecondMonthlyValue(vo.getSecondMonthlyValue());
				}
				resList.add(excelVo);
			}
		}
		//排序
		List<SupplyPlanVersionCompareExcelVO> sortList = sortSupplyCompareDetailList(resList);
		SupplyCompareReportTemplate reportTemplate = new SupplyCompareReportTemplate(deadline);
		reportTemplate.appendRows(sortList);
        try (OutputStream outputStream = response.getOutputStream()){
        	response.setContentType("application/vnd.ms-excel");
        	response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("供货对比报表-截止".concat(deadline.replace("-", "")).concat("-").concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHH"))).concat(".xlsx"), "utf-8"));
        	reportTemplate.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
        log.info("###############供货对比表导出   End: "+ LocalDateTime.now());
	}
	
	private List<SupplyPlanVersionCompareExcelVO> sortSupplyCompareDetailList(List<SupplyPlanVersionCompareExcelVO> resList) {
		List<SupplyPlanVersionCompareExcelVO> sortList =  resList.parallelStream().sorted((a, b) -> {
		    //区域
			String aRegionName = StrUtils.nvl(a.getAreaName());
		    String bRegionName = StrUtils.nvl(b.getAreaName());
		    if (!aRegionName.equals(bRegionName)) {
		        return aRegionName.compareTo(bRegionName);
		    }
		    //城市公司
		    String aCityName = StrUtils.nvl(a.getCityName());
		    String bCityName = StrUtils.nvl(b.getCityName());
		    if (!aCityName.equals(bCityName)) {
		        return aCityName.compareTo(bCityName);
		    }
		    //项目
		    String aProjectName = StrUtils.nvl(a.getProjectName());
		    String bProjectName = StrUtils.nvl(b.getProjectName());
		    if (!aProjectName.equals(bProjectName)) {
		        return aProjectName.compareTo(bProjectName);
		    }
		    //分期
		    String aStageName = StrUtils.nvl(a.getStageName());
		    String bStageName = StrUtils.nvl(b.getStageName());
		    if (!aStageName.equals(bStageName)) {
		    	return ReportUtils.compareStrStartedChinaNum(aStageName, bStageName);
		    }
		    
		    //组团
		    String aGroupName = StrUtils.nvl(a.getGroupName());
		    String bGroupName = StrUtils.nvl(b.getGroupName());
		    if (!aGroupName.equals(bGroupName)) {
		    	return ReportUtils.compareStrStartedChinaNum(aGroupName, bGroupName);
		    }
		    
		    //业态
		    String aProProductName = StrUtils.nvl(a.getProductTypeName());
		    String bProProductName = StrUtils.nvl(b.getProductTypeName());
		    if (!aProProductName.equals(bProProductName)) {
		        return aProProductName.compareTo(bProProductName);
		    }
		    //产品构成
		    String aProductName = StrUtils.nvl(a.getProductName());
		    String bProductName = StrUtils.nvl(b.getProductName());
		    if (!aProductName.equals(bProductName)) {
		        return aProductName.compareTo(bProductName);
		    }
		    return 0;
		}).collect(Collectors.toList());
		return sortList;
	}

	@Override
	public R supplyMonthlyDetailReport(String apiVersion, String deadline) {
		String userName = getOperator().getUsername();
		String localEndDate = LocalDate.now().format(DateTimeFormatter.ofPattern(DateUtils.DATE_JFP_STR));
	    String endDate = StrUtils.isEmpty(deadline) ? localEndDate : deadline;
	    deadline = dmDmSupplyMonthlyDetailService.getEndDate(endDate);
	    if(StrUtils.isEmpty(deadline)) {
	    	return ok(null);
	    }
		List<SupplyMonthlyDetailVO> list = dmDmSupplyMonthlyDetailService.querySupplyMonthlyDetail(deadline,userName);
		List<SupplyMonthlyDetailVO> sortList = list.parallelStream().sorted((a, b) -> {
		    //分期
		    String aStageName = StrUtils.nvl(a.getStageName());
		    String bStageName = StrUtils.nvl(b.getStageName());
		    if (!aStageName.equals(bStageName)) {
		    	return ReportUtils.compareStrStartedChinaNum(aStageName, bStageName);
		    }
		    return 0;
		}).collect(Collectors.toList());
		return ok(sortList);
	}

	@Override
	public void supplyMonthlyDetailReportExport(String apiVersion, String deadline,HttpServletResponse response) {
		log.info("###############月度供货明细表导出   Start: "+ LocalDateTime.now());
		String userName = getOperator().getUsername();
		List<SupplyMonthlyDetailExcelVO> resList = Collections.synchronizedList(new ArrayList<>());
		List<SupplyMonthlyDetailExcelVO> lastList = Collections.synchronizedList(new LinkedList<>());
		List<SupplyMonthlyDetailVO> list = dmDmSupplyMonthlyDetailService.querySupplyMonthlyDetail(deadline,userName);
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyMonthlyDetailVO detailVo : list) {
				SupplyMonthlyDetailExcelVO vo = new SupplyMonthlyDetailExcelVO();
				String type = detailVo.getType();
				vo.setId(detailVo.getId());
				vo.setCode(detailVo.getCode());
				vo.setName(detailVo.getName());
				vo.setPid(detailVo.getPid());
				vo.setType(detailVo.getType());
				vo.setIsParking(detailVo.getIsParking());
				if(type.equals(OrgTypeEunm.ORG_BUILD.getKey())) {
					vo.setVersionType(detailVo.getVersionType());
					vo.setBulidNo(detailVo.getBulidNo());
					vo.setBulidNo2(detailVo.getBulidNo2());
					vo.setPendingHomeSale2(detailVo.getPendingHomeSale2());
					vo.setBusinessTypeName(detailVo.getBusinessTypeName());
					vo.setFreeTypeName(detailVo.getFreeTypeName());
					vo.setProTypeName(detailVo.getProTypeName());
					//楼栋所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
					vo.setVersionType(detailVo.getVersionType());
					vo.setProductTypeName(detailVo.getProductTypeName());
					vo.setProductTypeCode(detailVo.getProductTypeCode());
					vo.setProductName(detailVo.getName());
					vo.setProductCode(detailVo.getCode());
					//产品构成所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_PRO_TYPE.getKey())) {
					vo.setVersionType(detailVo.getVersionType());
					vo.setProductTypeName(detailVo.getProductTypeName());
					vo.setProductTypeCode(detailVo.getProductTypeCode());
					//产品构成所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_GROUP.getKey())) {
					vo.setVersionType(detailVo.getVersionType());
					vo.setGroupName(detailVo.getName());
					//组团所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_STAGE.getKey())) {
					vo.setVersionType(detailVo.getVersionType());
					vo.setStageName(detailVo.getStageName());
					vo.setStageCode(detailVo.getStageCode());
					vo.setEquityRatio(detailVo.getEquityRatio());
					vo.setTradersWay(detailVo.getTradersWay());
					vo.setIsMergeTable(detailVo.getIsMergeTable());
					vo.setPendingHomeSale(detailVo.getPendingHomeSale());
					//分期所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_PRODUCTF.getKey())) {
					vo.setProjectName(detailVo.getName());
					vo.setProjectCode(detailVo.getCode());
					vo.setIsFirst(detailVo.getIsFirst());
					//项目所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_CITY_COMPANY.getKey())) {
					vo.setCityName(detailVo.getName());
					//城市公司所有父级信息
					prepareMonthlyParentInfo(list,detailVo.getPid(),vo,type);
				}else if(type.equals(OrgTypeEunm.ORG_BUSINESS_DIVISION.getKey())){
					//区域
					vo.setAreaName(detailVo.getName());
				}else if(type.equals(OrgTypeEunm.ORG_WANDA_GROUP.getKey())){
					//集团
					vo.setAreaName("集团合计");
					vo.setInventory(detailVo.getInventory());
					vo.setPlan(detailVo.getPlan());
					vo.setActual(detailVo.getActual());
					lastList.add(vo);
					continue;
				}
				vo.setInventory(detailVo.getInventory());
				vo.setPlan(detailVo.getPlan());
				vo.setActual(detailVo.getActual());
				resList.add(vo);
			}
			//排序
			List<SupplyMonthlyDetailExcelVO> sortList = sortSupplyMonthDetailExcelList(resList);
			lastList.addAll(sortList);
		}
		
		SupplyMonthlyDeatilReportTemplate reportTemplate = new SupplyMonthlyDeatilReportTemplate(deadline);
		reportTemplate.appendRows(lastList);
        try (OutputStream outputStream = response.getOutputStream()){
        	response.setContentType("application/vnd.ms-excel");
        	response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode("月度供货明细表-截止".concat(deadline.replace("-", "")).concat("-").concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHH"))).concat(".xls"), "utf-8"));
        	reportTemplate.write(outputStream);
            outputStream.flush();
        } catch (IOException e) {
            log.error("###### errorMsg: ", e);
        }
		
		log.info("###############月度供货明细表导出   End: "+ LocalDateTime.now());
	}

	private List<SupplyMonthlyDetailExcelVO> sortSupplyMonthDetailExcelList(List<SupplyMonthlyDetailExcelVO> resList) {
		List<SupplyMonthlyDetailExcelVO> sortList =  resList.parallelStream().sorted((a, b) -> {
			//区域
			String aRegionName = StrUtils.nvl(a.getAreaName());
		    String bRegionName = StrUtils.nvl(b.getAreaName());
		    if (!aRegionName.equals(bRegionName)) {
		        return aRegionName.compareTo(bRegionName);
		    }
		    //城市公司
		    String aCityName = StrUtils.nvl(a.getCityName());
		    String bCityName = StrUtils.nvl(b.getCityName());
		    if (!aCityName.equals(bCityName)) {
		        return aCityName.compareTo(bCityName);
		    }
		    //项目
		    String aProjectName = StrUtils.nvl(a.getProjectName());
		    String bProjectName = StrUtils.nvl(b.getProjectName());
		    if (!aProjectName.equals(bProjectName)) {
		        return aProjectName.compareTo(bProjectName);
		    }
		    //分期
		    String aStageName = StrUtils.nvl(a.getStageName());
		    String bStageName = StrUtils.nvl(b.getStageName());
		    if (!aStageName.equals(bStageName)) {
		    	return ReportUtils.compareStrStartedChinaNum(aStageName, bStageName);
		    }
		    //组团
		    String aGroupName = StrUtils.nvl(a.getGroupName());
		    String bGroupName = StrUtils.nvl(b.getGroupName());
		    if (!aGroupName.equals(bGroupName)) {
		    	return ReportUtils.compareStrStartedChinaNum(aGroupName, bGroupName);
		    }
		    //业态
		    String aProProductName = StrUtils.nvl(a.getProductTypeName());
		    String bProProductName = StrUtils.nvl(b.getProductTypeName());
		    if (!aProProductName.equals(bProProductName)) {
		        return aProProductName.compareTo(bProProductName);
		    }
		    //产品构成
		    String aProductName = StrUtils.nvl(a.getProductName());
		    String bProductName = StrUtils.nvl(b.getProductName());
		    if (!aProductName.equals(bProductName)) {
		        return aProductName.compareTo(bProductName);
		    }
		    //楼栋
		    String aBuildingName = StrUtils.nvl(a.getBulidNo2());
            String bBuildingName = StrUtils.nvl(b.getBulidNo2());
            if (!aBuildingName.equals(bBuildingName)) {
                return ReportUtils.compareStrStartedChinaNum(aBuildingName, bBuildingName);
            }
		    return 0;
		}).collect(Collectors.toList());
		return sortList;
	}

	private void prepareCompareParentInfo(List<SupplyPlanVersionCompareVO> list, String pId,
			SupplyPlanVersionCompareExcelVO vo,String type) {
		if(type.equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
			//组团
			SupplyPlanVersionCompareVO gVo = getCompareParentData(list,pId);
			//分期
			SupplyPlanVersionCompareVO sVo = getCompareParentData(list,gVo.getPid());
			//项目
			SupplyPlanVersionCompareVO pjVo = getCompareParentData(list,sVo.getPid());
			//城市公司
			SupplyPlanVersionCompareVO ctVo = getCompareParentData(list,pjVo.getPid());
			//区域
			SupplyPlanVersionCompareVO areaVo = getCompareParentData(list,ctVo.getPid());
			
			//组团
			vo.setGroupName(gVo.getName());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getProjectName());
			vo.setProjectCode(pjVo.getProjectCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getCityName());
			//事业部（区域）
			vo.setAreaName(areaVo.getAreaName());
		}else if(type.equals(OrgTypeEunm.ORG_GROUP.getKey())) {
			//分期
			SupplyPlanVersionCompareVO sVo = getCompareParentData(list,pId);
			//项目
			SupplyPlanVersionCompareVO pjVo = getCompareParentData(list,sVo.getPid());
			//城市公司
			SupplyPlanVersionCompareVO ctVo = getCompareParentData(list,pjVo.getPid());
			//区域
			SupplyPlanVersionCompareVO areaVo = getCompareParentData(list,ctVo.getPid());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getProjectName());
			vo.setProjectCode(pjVo.getProjectCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getCityName());
			//事业部（区域）
			vo.setAreaName(areaVo.getAreaName());
		}else if(type.equals(OrgTypeEunm.ORG_STAGE.getKey())) {
			//项目
			SupplyPlanVersionCompareVO pjVo = getCompareParentData(list,pId);
			//城市公司
			SupplyPlanVersionCompareVO ctVo = getCompareParentData(list,pjVo.getPid());
			//区域
			SupplyPlanVersionCompareVO areaVo = getCompareParentData(list,ctVo.getPid());
			//项目
			vo.setProjectName(pjVo.getProjectName());
			vo.setProjectCode(pjVo.getProjectCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getCityName());
			//事业部（区域）
			vo.setAreaName(areaVo.getAreaName());
		}else if(type.equals(OrgTypeEunm.ORG_PRODUCTF.getKey())) {
			//城市公司
			SupplyPlanVersionCompareVO ctVo = getCompareParentData(list,pId);
			//区域
			SupplyPlanVersionCompareVO areaVo = getCompareParentData(list,ctVo.getPid());
			//城市公司
			vo.setCityName(ctVo.getCityName());
			//事业部（区域）
			vo.setAreaName(areaVo.getAreaName());
		}else if(type.equals(OrgTypeEunm.ORG_CITY_COMPANY.getKey())) {
			//区域
			SupplyPlanVersionCompareVO areaVo = getCompareParentData(list,pId);
			//事业部（区域）
			vo.setAreaName(areaVo.getAreaName());
		}
	}
	
	private void prepareMonthlyParentInfo(List<SupplyMonthlyDetailVO> list, String pId, SupplyMonthlyDetailExcelVO vo,String type) {
		if(type.equals(OrgTypeEunm.ORG_BUILD.getKey())) {
			//产品构成
			SupplyMonthlyDetailVO pVo = getParentData(list,pId);
			
			SupplyMonthlyDetailVO proVo = getParentData(list,pVo.getPid());
			//组团
			SupplyMonthlyDetailVO gVo = getParentData(list,proVo.getPid());
			//分期
			SupplyMonthlyDetailVO sVo = getParentData(list,gVo.getPid());
			//项目
			SupplyMonthlyDetailVO pjVo = getParentData(list,sVo.getPid());
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pjVo.getPid());
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			//产品构成
			vo.setProductName(pVo.getName());
			vo.setProductCode(pVo.getCode());
			//业态
			vo.setProductTypeName(proVo.getName());
			vo.setProductTypeCode(proVo.getCode());
			//组团
			vo.setGroupName(gVo.getName());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getName());
			vo.setProjectCode(pjVo.getCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_PRODUCT.getKey())) {
			//业态
			SupplyMonthlyDetailVO proVo = getParentData(list,pId);
			//组团
			SupplyMonthlyDetailVO gVo = getParentData(list,proVo.getPid());
			//分期
			SupplyMonthlyDetailVO sVo = getParentData(list,gVo.getPid());
			//项目
			SupplyMonthlyDetailVO pjVo = getParentData(list,sVo.getPid());
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pjVo.getPid());
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			
			//业态
			vo.setProductTypeName(proVo.getName());
			vo.setProductTypeCode(proVo.getCode());
			//组团
			vo.setGroupName(gVo.getName());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getName());
			vo.setProjectCode(pjVo.getCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_PRO_TYPE.getKey())) {
			//组团
			SupplyMonthlyDetailVO gVo = getParentData(list,pId);
			//分期
			SupplyMonthlyDetailVO sVo = getParentData(list,gVo.getPid());
			//项目
			SupplyMonthlyDetailVO pjVo = getParentData(list,sVo.getPid());
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pjVo.getPid());
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			
			//组团
			vo.setGroupName(gVo.getName());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getName());
			vo.setProjectCode(pjVo.getCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_GROUP.getKey())) {
			//分期
			SupplyMonthlyDetailVO sVo = getParentData(list,pId);
			//项目
			SupplyMonthlyDetailVO pjVo = getParentData(list,sVo.getPid());
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pjVo.getPid());
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			//分期
			vo.setStageName(sVo.getStageName());
			vo.setStageCode(sVo.getStageCode());
			vo.setEquityRatio(sVo.getEquityRatio());
			vo.setTradersWay(sVo.getTradersWay());
			vo.setIsMergeTable(sVo.getIsMergeTable());
			vo.setPendingHomeSale(sVo.getPendingHomeSale());
			//项目
			vo.setProjectName(pjVo.getName());
			vo.setProjectCode(pjVo.getCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_STAGE.getKey())) {
			//项目
			SupplyMonthlyDetailVO pjVo = getParentData(list,pId);
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pjVo.getPid());
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			//项目
			vo.setProjectName(pjVo.getName());
			vo.setProjectCode(pjVo.getCode());
			vo.setIsFirst(pjVo.getIsFirst());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_PRODUCTF.getKey())) {
			//城市公司
			SupplyMonthlyDetailVO ctVo = getParentData(list,pId);
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,ctVo.getPid());
			//城市公司
			vo.setCityName(ctVo.getName());
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}else if(type.equals(OrgTypeEunm.ORG_CITY_COMPANY.getKey())) {
			//事业部（区域）
			SupplyMonthlyDetailVO areaVo = getParentData(list,pId);
			//事业部（区域）
			vo.setAreaName(areaVo.getName());
		}
	}
	
	private SupplyPlanVersionCompareVO getCompareParentData(List<SupplyPlanVersionCompareVO> list,String pId) {
		SupplyPlanVersionCompareVO vo = new SupplyPlanVersionCompareVO();
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyPlanVersionCompareVO detail : list) {
				if(detail.getId().equals(pId)) {
					vo = detail;
				}
			}
		}
		return vo;
	}
	
	private SupplyMonthlyDetailVO getParentData(List<SupplyMonthlyDetailVO> list,String pId) {
		SupplyMonthlyDetailVO vo = new SupplyMonthlyDetailVO();
		if(CollectionUtils.isNotEmpty(list)) {
			for(SupplyMonthlyDetailVO detail : list) {
				if(detail.getId().equals(pId)) {
					vo = detail;
				}
			}
		}
		return vo;
	}

	@Override
	public R initSupplyConfirmReport(String apiVersion, String yearMonth, String projectId) {
		log.info("###############刷新供货确认表   Start: "+ LocalDateTime.now());
		long start = System.currentTimeMillis();
		supplyConfirmRecordDetailService.initSupplyConfirmReport(yearMonth,projectId);
		long end = System.currentTimeMillis();
		long diff = end-start;
		log.info("###############刷新供货确认表   End: "+ LocalDateTime.now());
		return ok("运行时长（毫秒）："+diff);
	}

}
